<?php
require("bases.php");

class mybasic extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$this->fmenu = $this->controller = "mybasic";
			$this->position_one = array("text"=>"管理员中心");
		}catch(Exception $e){

		}
	}
	
	function updatepwd(){
		$id = $_SESSION["admin_user"]["aid"];
		if($id){
			$conditions = array('aid'=>$id);
			$admin = spClass('admin');
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					$postdata = $this->spArgs();
					$data = array();
					$data["oldpassword"] = $postdata["oldpassword"];
					$data["password"] = $postdata["password"];
					$data["password1"] = $postdata["password1"];
					//客户端提交方法
					if(!$data["oldpassword"])
						throw new Exception("原始密码不能为空");
					if(!$data['password'])
						throw new Exception("请输入新密码");
					if(!$data['password1'])
						throw new Exception("请输入确认密码");
					if($result = $admin->spValidatoredit()->spValidator($data)) {
						foreach($result as $item) {
							throw new Exception($item[0]);
						}
					}
					if($data['password'] != $data['password1'])
						throw new Exception("两次密码不一致");
					if(!$admin->find(array("aid"=>$id, "password"=>md5($data["oldpassword"]))))
						throw new Exception("原始密码不正确，请重新输入");
					$url = spUrl($this->controller, "updatepwd");
					$newrow["password"] = md5($data['password']);
					if(!$admin->update($conditions, $newrow))
						throw new Exception("未知错误，密码更新失败");
					$message = array('msg'=>'修改成功！', 'result'=>1, 'url'=>$url);
					echo json_encode($message);
					exit();
				}catch(Exception $e){
					$message = array('msg' => $e->getmessage(), 'result'=>0);
					echo json_encode($message);
					exit();
				}
		    }
		    $this->validator = $admin->getValidatorJSedit();
		    $this->admin_rs = $admin->find($conditions);
	 	    $this->saveurl = spUrl("mybasic", "updatepwd", array("id" => $id));
			$this->position_two = array("text"=>"更新密码", "url"=>spUrl($this->controller, "updatepwd"));
			$this->formtitle = array("text"=>"更新密码");
		}else{
			$this->redirect(spUrl("main", "index"), "非法操作！");
		}
	}
}
?>